package action;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import service.StudentsDAO;
import service.impl.StudentsDAOImpl;
import entity.Students;

/**
 * 学生Action类
 * @author thinkpad
 *
 */
public class StudentsAction extends SuperAction {


	private static final long serialVersionUID = 1L;
	
	/**
	 * 查询所有学生的动作
	 * @return
	 */
	public String query()
	{
		StudentsDAO sDao = new StudentsDAOImpl();
		List<Students> list = sDao.queryAllStudents();
		
		if (null!=list&&list.size()>0) {
			session.setAttribute("students_list", list);
		}
		return "query_success";
	}

	/**
	 * 根据学生编号查询学生资料
	 * @param sid
	 * @return
	 */
	public Students queryStudentsBySid()
	{
		Students students = null;
		StudentsDAO sDao = new StudentsDAOImpl();
		String sid = request.getParameter("sid");
		students=sDao.queryStudentsBySid(sid);
		return students;
	}
	/**
	 * 添加学生资料
	 * @param s
	 * @return
	 */
	public String add() throws Exception
	{
		Students students = new Students();
		students.setSname(request.getParameter("sname"));
		students.setGender(request.getParameter("gender"));
		
		SimpleDateFormat sdfDateFormat = new SimpleDateFormat("yyyy-MM-dd");
		students.setBirthday(sdfDateFormat.parse(request.getParameter("birthday")));
		students.setAddress(request.getParameter("address"));
		StudentsDAO sDao = new StudentsDAOImpl();
		sDao.addStudents(students);
		return "add_success";
	}
	/**
	 * 修改学生资料
	 * @param s
	 * @return
	 */
	public String modify()
	{
		String sidString = request.getParameter("sid");
		StudentsDAO sDao=new StudentsDAOImpl();
		Students s = sDao.queryStudentsBySid(sidString);
		session.setAttribute("modify_students", s);
		return "modify_success";
	}
	/**
	 * 删除学生资料
	 * @param sid
	 * @return
	 */
	public String delete()
	{
		StudentsDAO sDao=new StudentsDAOImpl();
		String sidString = request.getParameter("sid");
		sDao.deleteStudents(sidString);
		return "delete_success";
	}
	
	public String save() throws Exception
	{
		Students students = new Students();
		students.setSid(request.getParameter("sid"));
		students.setSname(request.getParameter("sname"));
		students.setGender(request.getParameter("gender"));
		
		SimpleDateFormat sdfDateFormat = new SimpleDateFormat("yyyy-MM-dd");
		students.setBirthday(sdfDateFormat.parse(request.getParameter("birthday")));
		students.setAddress(request.getParameter("address"));
		StudentsDAO sDao = new StudentsDAOImpl();
		sDao.updateStudents(students);
		return "save_success";
	}
}
